﻿using UnityEngine;

namespace Runtime {

    public interface IDFR_BattleSpeeder { 
        
        void SetSpeed(float speed);
    }

    [DisallowMultipleComponent]
    public sealed class DFR_BattleParticleSystemSpeeder : MonoBehaviour, IDFR_BattleSpeeder {

        private DFR_BattleParticleSystemSpeeder() : base() { }

        private ParticleSystem[] m_ParticleSystems;

        private bool m_HasSpeed;
        private float m_Speed;

        private void Awake() {
            m_ParticleSystems = gameObject.GetComponentsInChildren<ParticleSystem>();
            if (m_HasSpeed) {
                SetSpeed(m_Speed);
            }
        }

        public void SetSpeed(float value) {
            m_Speed = value;
            m_HasSpeed = true;
            if (m_ParticleSystems != null) {
                foreach (var particleSystem in m_ParticleSystems) {
                    var main = particleSystem.main;
                    main.simulationSpeed = value;
                }
            }
        }

        private void OnDestroy() {
            m_ParticleSystems = null;
        }
    }
}
